<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {

    public function _initialize() {
        $name = M('theme')->where( array('is_default'=>1) )->getField('name');
        $this->theme(C('NOW_THEME'));
        $this->assign('theme', C('NOW_THEME'));
    }

    //用户注册
    public function register()
    {
        // 查询是否已经开启手机验证码
        $open_code = M("config")->where("conf_key = 'open_code' AND conf_type = 'shop'")
            ->getField("conf_value");
        if (IS_AJAX)
        {
            $reg_code = trim(I('post.reg_code')); // 注册码
            $phone = I('post.phone');
            $shop_id = I('post.shop_id');
            // 开启手机验证码
            if($open_code == 1){
                $phone_code = session('sms_code'); // 手机验证码
                $code = trim(I('post.code')); // 用户填写的验证码
                if(!$code) $this->ajaxReturn(array("status" => "error" ,"msg" => "验证码不能为空"));
                if($code != $phone_code) $this->ajaxReturn(array("status" => "error" ,"msg" => "验证码错误"));
                session('sms_code' ,null);
            }

            if($reg_code != ""){
                $agent = M('agent')->where( array('mobile' => $phone, 'invitation_code' => $reg_code,'shop_id' => $shop_id))->find();
                if ( !$agent ) {
                    $this->ajaxReturn(array("status" => "error" ,"msg" => "注册码与手机号不匹配"));
                }
                $agent_id = $agent['agent_id'];

            }else{
                //没有注册码
                $isset_agent = M('agent')->where( array('mobile' => $phone,'shop_id' => $shop_id) )->find();
                if ( $isset_agent ) {
                    $this->ajaxReturn(array("status" => "error" ,"msg" => "此号码已存在"));
                }
                M('agent')->add(array(
                    "agent_name" => $phone,
                    "mobile" => $phone,
                    "shop_id" => session("url_shop_id")
                ));
                $agent_id = M('agent')->getLastInsId();
            }
            // 查询注册是否需要审核
            $conf_value = M('config')
                ->where( array('conf_key' => 'agent_check' ,"conf_type" => "shop") )
                ->getField('conf_value');
            $u_status = 1;
            if($conf_value == 1){
                $u_status = 2;
            }
            // 写入注册信息
            $salt = substr(md5(time() . $phone) ,-8);
            $password = md5(md5( I('post.password'))  .$salt );
            M('agent_info')->add(array(
                'agent_id' => $agent_id,
                'password' => $password,
                'salt' => $salt,
                'reg_time' => date("Y-m-d H:i:s",time()),
                'reg_ip' => get_client_ip()
            ));

            //把代理表的邀请码修改成*，代码注册成功。读取是否需要审核。
            M('agent')->where(array('mobile' => $phone, 'invitation_code' => $reg_code,'shop_id'=>session("url_shop_id")))->save(array(
                'invitation_code' => '*',
                'u_status' => $u_status
            ));

            //注册直接进入到系统
            $agent = M('agent')->where(array('agent_id'=>$agent_id))->find();
            $agentInfo = M('agent_info')->where(array('agent_id'=>$agent_id))->find();
            session('user_id', $agentInfo['agent_id']);
            session('aavatar', $agentInfo['avatar']);
            session('auser_name', $agent['agent_name']);
            session('amobile', $agent['mobile']);
            session('is_agent',$agent['is_agent']);
            session('reception_shopid', $agent['shop_id']);

            $this->ajaxReturn(array("status" => "success"));
        } else {
            $url_shop_id = I(trim('get.shop_id'));
            $shop = M('shop')->where( array('id'=>$url_shop_id,'state'=>1) )->find();
            if (empty($shop)) {
                exit('不存在此店铺！');
            }
            $agmt = I("get.agmt");
            $this->assign("agmt" ,$agmt);
            $this->assign("shop_id" ,$url_shop_id);
            $this->assign("open_code" ,$open_code);
            $this->site_title = "注册";
            $this->display();
        }
    }

    //注册发送的验证码
    public function sms()
    {
        $code = generate_code();
        // 设置过期时间 60s
        session(array( 'expire' => 100, 'name' => 'sms_code' ));
        session('sms_code', $code);
        $phone = I('post.phone');
        $code = array("code" => $code);
        $info = send_sms($phone, $code ,1);
        $this->ajaxReturn($info);
    }

    // 用户协议
    public function agreement()
    {
        $url_shop_id = I(trim('get.shop_id'));
        $shop = M('shop')->where( array('id'=>$url_shop_id ,'state'=> 1) )->find();
        if (empty($shop)) {
            exit('不存在此店铺！');
        }
        $this->assign("shop_id" ,$url_shop_id);
        $this->display();
    }

}